package com.kylingroup.JavaFX.TreeView;

import java.io.File;
import java.util.HashMap;
import javafx.scene.Node;
import javafx.scene.control.TreeItem;

public class DirectoryIterator{
    private File files[]=null;
    private TreeItem<FileTreeItem> root=null;
    private HashMap<String,Node> fileInfoNodes=null;
    private String filter="";
            
    public DirectoryIterator(TreeItem<FileTreeItem> root,HashMap<String,Node> fileInfoNodes){
        this.root=root;
        this.fileInfoNodes=fileInfoNodes;
    }
    
    public boolean start(String path,String filter){
        this.filter=filter;
        boolean status=false;
        File file=new File(path);
        if(!file.isDirectory()){
            status=false;
        }else{
            files=file.listFiles();
            for(int i=0;i<files.length;i++){
                File sub=files[i];

                if(filter!=null&&!filter.equals("")){
                    if(!sub.isDirectory()){
                        String type=sub.getName();
                        type=type.substring(type.lastIndexOf(".")+1,type.length());
                        if(type.equalsIgnoreCase(filter)){
                            FileTreeItem subItem=new FileTreeItem();
                            subItem.setFile(sub);
                            subItem.setFileInfoNodes(fileInfoNodes);
                            TreeItem<FileTreeItem> treeItem=new TreeItem<FileTreeItem>(subItem);
                            root.getChildren().add(treeItem);
                        }
                    }else{
                        iterator(sub.getAbsolutePath(),root);
                    }
                }else{
                    if(sub.isDirectory()){
                        FileTreeItem subItem=new FileTreeItem();
                        subItem.setFile(sub);

                        TreeItem<FileTreeItem> treeItem=new TreeItem<FileTreeItem>(subItem);
                        root.getChildren().add(treeItem);
                        iterator(sub.getAbsolutePath(),treeItem);
                    }else{
                        FileTreeItem subItem=new FileTreeItem();
                        subItem.setFile(sub);
                        subItem.setFileInfoNodes(fileInfoNodes);
                        TreeItem<FileTreeItem> treeItem=new TreeItem<FileTreeItem>(subItem);
                        root.getChildren().add(treeItem);
                    }
                }
            }
            status=true;
        }
        return status;
    }
    
    public void iterator(String path,TreeItem<FileTreeItem> subtreeItem){
        File file=new File(path);
        File files[]=file.listFiles();
        for(int i=0;i<files.length;i++){
            File sub=files[i];

            if(filter!=null&&!filter.equals("")){
                if(!sub.isDirectory()){
                    String type=sub.getName();
                    type=type.substring(type.lastIndexOf(".")+1,type.length());
                    if(type.equalsIgnoreCase(filter)){
                        FileTreeItem subItem=new FileTreeItem();
                        subItem.setFile(sub);
                        subItem.setFileInfoNodes(fileInfoNodes);  
                        TreeItem<FileTreeItem> treeItem=new TreeItem<FileTreeItem>(subItem);
                        subtreeItem.getChildren().add(treeItem);
                    }
                }else{
                    iterator(sub.getAbsolutePath(),subtreeItem);
                }
            }else{
                if(sub.isDirectory()){
                    FileTreeItem subItem=new FileTreeItem();
                    subItem.setFile(sub);

                    TreeItem<FileTreeItem> treeItem=new TreeItem<FileTreeItem>(subItem);
                    subtreeItem.getChildren().add(treeItem);
                    iterator(sub.getAbsolutePath(),treeItem);
                }else{
                    FileTreeItem subItem=new FileTreeItem();
                    subItem.setFile(sub);
                    subItem.setFileInfoNodes(fileInfoNodes);  
                    TreeItem<FileTreeItem> treeItem=new TreeItem<FileTreeItem>(subItem);
                    subtreeItem.getChildren().add(treeItem);
                }
            }
        }
    }
}
